;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.TerminalSalesDetail", [])
        .controller("TerminalSalesDetailCtrl", ["$scope", "ybWidget", "$timeout", "ybHttp", "$filter", "MESG_TYPE", function ($scope, ybWidget, $timeout, ybHttp, $filter, MESG_TYPE) {
            // 搜索条件
            $scope.condition = {};

            $scope.datePickerFlag = [false, false];
            // 切换时间展示 隐藏
            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };
            // 检查日期
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            // 高级搜索的展示与隐藏
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
            };
            // 切换高级搜索展示与隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig[toolbar]) {
                    $scope.toolbarConfig[toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig[toolbar] = true;
                    });
                }
            };
            // 重置搜索条件
            $scope.reset = function () {
                $scope.condition = {};
            };
            // 导出
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", $scope.isCHY ? '/report/exportAgentSalesOutDetail4Cy' : '/report/exportAgentSalesOutDetail')
            };

            function setTableHead() {
                $scope.tbConf = {
                    scrollWidth: 1070,
                    tableCol: [
                        {
                            type: "",
                            name: "代理商名称",
                            dataField: "agentName",
                            width: 180,
                            isShow: $scope.isCoreCompany,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "客户名称",
                            dataField: "clientName",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            name: "是否竞品客户",
                            dataField: "isCompetingDealerShow",
                            width: 110,
                            isShow: $scope.isWJ,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "联系方式",
                            dataField: "contactPhone",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "所属单位",
                            dataField: "shortName",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "客户分类",
                            dataField: "className",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "客户分级",
                            dataField: "dealerLevelName",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "是否大客户",
                            dataField: "bigClientFlagF",
                            width: 90,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "客户状态",
                            dataField: "normalFlagF",
                            width: 90,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "车辆编码",
                            dataField: "useMachineNo",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "保修状态",
                            dataField: "warrantyStatusF",
                            width: 90,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "零件编号",
                            dataField: "materialNo",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "零件中文名",
                            dataField: "materialDes",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "零件分类（二级）",
                            dataField: "materialClassName",
                            width: 130,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "进货基准价",
                            dataField: "price",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "零售目录价",
                            dataField: "guidePrice",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "成本价",
                            dataField: "standardPrice",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "终端销售价",
                            dataField: "singlePrice",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "出库数量",
                            dataField: "outboundQty",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "销售金额",
                            dataField: "amount",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "出库单号",
                            dataField: "salesOutBillNo",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "销售类型",
                            dataField: "billTypeF",
                            width: 90,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "销售人员",
                            dataField: "servicePersonName",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "销售时间",
                            dataField: "outboundDateF",
                            width: 140,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "出库仓库描述",
                            dataField: "outArea",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "销售渠道",
                            dataField: "saleTypeF",
                            width: 120,
                            isShow: true,
                            isFixed: false,
                        },
                        {
                            type: "",
                            name: "结算状态",
                            dataField: "checkStateF",
                            width: 90,
                            isShow: true,
                            isFixed: false,
                        },
                    ]
                };
            }

            $scope.markInfo = {
                curRow: -1
            };
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.tdClick = function (item, fnType) {
                // 如果有超链接，写在此处
                switch (fnType) {
                    case "viewDetail":
                        break;
                    default:
                        break;
                }
            };

            $scope.dataList = [];

            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl = $scope.isCHY ? '/report/agentSalesOutDetailList4Cy' : "/report/agentSalesOutDetailList";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.isCoreCompany = resp.coreCompanyFlag == 1;
                    setTableHead();
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    angular.forEach($scope.dataList, function (item) {
                        item.isCompetingDealerShow = $filter('TrueOrFalseOrNull')(item.isCompetingDealer);
                        item.bigClientFlagF = $filter('TrueOrFalseOrNull')(item.bigClientFlag);
                        item.normalFlagF = $filter('CompanyDealerStatus')(item.normalFlag);
                        item.warrantyStatusF = $filter('VehStatus')(item.warrantyStatus);
                        item.billTypeF = $filter('BillTypeStatus')(item.billType);
                        item.outboundDateF = $filter('YbDate')(item.outboundDate, "yyyy-MM-dd HH:mm:ss");
                        item.saleTypeF = $filter('SaleType')(item.saleType);
                        item.checkStateF = $filter('ProgressStatus')(item.checkState, "checkState");
                    });
                });
            }

            $scope.selectMaterialClass = function () {
                var checkedNames = []; // 已选中的name集合
                var checkedIds = []; // 已选中的id集合
                angular.forEach($scope.materialClassList, function (item) {
                    if (item.isChecked) {
                        checkedNames.push(item.name);
                        checkedIds.push(item.id);
                    }
                });
                $scope.checkedNameList = checkedNames.join(',');
                $scope.condition.materialClassIdList = checkedIds;
            };

            // 获取动态表格头
            function getMaterialClass() {
                var url = '/materialClass/getMaterialClassTreeList';
                ybHttp.Get('device', url).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    var searchTableCol = [];
                    angular.forEach(res.value, function (item) {
                        if (item.materialClassVOList.length > 0) {
                            angular.forEach(item.materialClassVOList, function (second) {
                                searchTableCol.push({
                                    name: second.materialCalssName,
                                    isChecked: true,
                                    id: second.id,
                                    value: second.id
                                })
                            });
                        }
                    });
                    $scope.materialClassList = searchTableCol;
                })
            }

            (function () {
                getMaterialClass();
                getDataList();
            })();


        }])
        ;
})(window, window.angular);